package com.hxd.otc.module.order.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.order.activity.ComplainListActivity;
import com.hxd.otc.module.order.bean.RedPointBean;
import com.hxd.otc.module.order.contract.OrderContract;
import com.hxd.otc.module.order.presenter.OrderPresenter;
import com.hxd.otc.widget.TitleBar;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/6/4 17:04
 * 描  述 :  ${订单页面}
 */
public class OrderFragment extends LazyFragment<OrderPresenter> implements OrderContract.View {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    List<Fragment> mFragmentList;

    String[] tabNames;

    public static final String ORDER_STATUES = "statues";

    AllOrderFragment allOrderFragment;
    AllOrderFragment unPayOrderFragment;
    AllOrderFragment payedOrderFragment;
    //    AllOrderFragment apealOrderFragment;
    AllOrderFragment cancledOrderFragment;
    AllOrderFragment finishedOrderFragment;

    boolean isNormalOrder = true;//是广告商 订单列表还是 普通订单列表

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected OrderPresenter createPresenter() {
        return new OrderPresenter(this);
    }

    @Override
    protected void initView() {
        tabNames = getResources().getStringArray(R.array.order_tab_name);
        initViewPager();

    }

    @Override
    protected void initData() {
        registerEventBus();
        mPresenter.redPointFlag();
    }

    private void initViewPager() {

        addFragments();

        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabNames[position];
            }
        });

        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setCurrentItem(0, false);
        mTabLayout.setupWithViewPager(mViewPager, true);

        setupTabIcons();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextSize(16);
                tabText.setTextColor(getResources().getColor(R.color.color_text1));
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.VISIBLE);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextSize(13);
                tabText.setTextColor(getResources().getColor(R.color.color_FFB1B1B1));
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mViewPager.setCurrentItem(position, false);
                switch (position) {
                    case 0:
                        allOrderFragment.refreshData(-1);
                        break;
                    case 1:
                        unPayOrderFragment.refreshData(0);
                        break;
                    case 2:
                        payedOrderFragment.refreshData(1);
                        break;
                    case 3:
                        cancledOrderFragment.refreshData(9);
                        break;
                    case 4:
                        finishedOrderFragment.refreshData(2);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addFragments() {
        mFragmentList = new ArrayList<>();
        if (allOrderFragment == null) {
            allOrderFragment = AllOrderFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putInt(ORDER_STATUES, -1);
            bundle.putBoolean("isNor", isNormalOrder);
            allOrderFragment.setArguments(bundle);
            mFragmentList.add(allOrderFragment);
        }
        if (unPayOrderFragment == null) {
            unPayOrderFragment = AllOrderFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putInt(ORDER_STATUES, 0);
            bundle.putBoolean("isNor", isNormalOrder);
            unPayOrderFragment.setArguments(bundle);
            mFragmentList.add(unPayOrderFragment);
        }
        if (payedOrderFragment == null) {
            payedOrderFragment = AllOrderFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putInt(ORDER_STATUES, 1);
            bundle.putBoolean("isNor", isNormalOrder);
            payedOrderFragment.setArguments(bundle);
            mFragmentList.add(payedOrderFragment);
        }

//
        if (cancledOrderFragment == null) {
            cancledOrderFragment = AllOrderFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putInt(ORDER_STATUES, 9);
            bundle.putBoolean("isNor", isNormalOrder);
            cancledOrderFragment.setArguments(bundle);
            mFragmentList.add(cancledOrderFragment);
        }
        if (finishedOrderFragment == null) {
            finishedOrderFragment = AllOrderFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putInt(ORDER_STATUES, 2);
            bundle.putBoolean("isNor", isNormalOrder);
            finishedOrderFragment.setArguments(bundle);
            mFragmentList.add(finishedOrderFragment);
        }
    }

    private void setupTabIcons() {
        for (int i = 0; i < mFragmentList.size(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(getTabView(i));//给每一个tab设置view
        }
    }

    TextView tvRedUnPay;
    TextView tvRedPayed;

    public View getTabView(int position) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_tab, null);
        TextView tv_bottom = view.findViewById(R.id.tv_bottom);
        tv_bottom.setText(tabNames[position]);
        tv_bottom.setTextColor(position == 0 ? getResources().getColor(R.color.color_text1) : getResources().getColor(R.color.color_FFB1B1B1));
        ImageView ivBotton = view.findViewById(R.id.iv_botton);
        if (position == 0) {
            ivBotton.setVisibility(View.VISIBLE);
            tv_bottom.setTextSize(16);
        } else {
            ivBotton.setVisibility(View.GONE);
            tv_bottom.setTextSize(13);
        }
        if (position == 1) {
            tvRedUnPay = view.findViewById(R.id.tv_red);
        }
        if (position == 2) {
            tvRedPayed = view.findViewById(R.id.tv_red);
        }
        return view;

    }

    @Override
    protected void initEvent() {

        titleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {

            }

            @Override
            public void onRight(View v) {
                $startActivity(ComplainListActivity.class);
            }
        });
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);

    }


    @Override
    public void getPointFlag(RedPointBean bean) {
        tvRedUnPay.setVisibility(bean.noPayDetailCount > 0 ? View.VISIBLE : View.GONE);
        tvRedPayed.setVisibility(bean.payDetailCount > 0 ? View.VISIBLE : View.GONE);
        tvRedUnPay.setText(bean.noPayDetailCount + "");
        tvRedPayed.setText(bean.payDetailCount + "");
        postSticky(new Event.RefreshMainRedEvent(bean.noPayDetailCount + bean.payDetailCount));//确认收款的 订单状态一定是发生变化的,通知更新红点
    }

    /**
     * 当订单状态改变时，重新请求数据
     *
     * @param refreshOrderRedEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(Event.RefreshOrderRedEvent refreshOrderRedEvent) {
        if (refreshOrderRedEvent.getStatues() == 0 || refreshOrderRedEvent.getStatues() == 1) {
            mPresenter.redPointFlag();
        }
    }
}
